public class Test {
    public static void main(String[] args) {
        int[] nums = {0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1};
        Solution solution = new Solution();
        int k = solution.longestOnes(nums, 3);
        System.out.println(k);
    }
}

class Solution {
    public int longestOnes(int[] nums, int k) {
        //
        int left = 0, right = 0;
        int len = 0;
        int count = k;
        if(nums[right] == 0) {
            count--;
        }
        while (right < nums.length) {
            right++;
            if(right==nums.length){//防止数组越界
                break;
            }
            if(nums[right] == 0) {
                count--;
            }
            while (count < 0) {
                if(nums[left]==0)
                    count++;
                left++;
            }
            len=Math.max(len, right - left+1);

        }
        return len;
    }
}